package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.biz.helper.ServiceHelper;
import com.zjdiepu.www.dao.base.OtherAttachmentsMapper;
import com.zjdiepu.www.dao.dz.DzPickCarMapper;
import com.zjdiepu.www.dao.fk.FkAuditListMapper;
import com.zjdiepu.www.dao.other.OtherAuditInfoMapper;
import com.zjdiepu.www.dao.other.OtherWantWarningMapper;
import com.zjdiepu.www.dao.yw.YwBusinessCarMapper;
import com.zjdiepu.www.dao.yw.YwBusinessExtrasMapper;
import com.zjdiepu.www.dao.yw.YwBusinessLoanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.fk.IFkAuditListService;
import com.zjdiepu.www.ibiz.system.IBaseSysParamsService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessCarService;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.ext.YwBusinessCarExt;

/**
 * 车辆信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 19时42分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwBusinessCarServiceImpl extends BaseService implements IYwBusinessCarService {
	public static final Logger logger = LoggerFactory.getLogger(YwBusinessCarServiceImpl.class);

	@Autowired
	private YwBusinessCarMapper ywBusinessCarMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private YwBusinessExtrasMapper ywBusinessExtrasMapper;
	@Autowired
	private DzPickCarMapper dzPickCarMapper;
	@Autowired
	private OtherAttachmentsMapper otherAttachmentsMapper;
	@Resource
	private OtherWantWarningMapper otherWantWarningMapper;
	@Resource
	private IBaseSysParamsService baseSysParamsService;
	@Resource
	private ServiceHelper serviceHelper;
//	@Resource
//	private FkInsureListMapper fkInsureListMapper;
//	@Resource
//	private ICachedDictionaryService cachedDictionaryService;
//	@Resource
//	private BaseCustomerMapper baseCustomerMapper;
//	@Resource
//	private BaseSalersmanMapper baseSalersmanMapper;
//	@Resource
//	private BaseBusiUnitMapper baseBusiUnitMapper;
//	@Resource
//	private DpBaseDealerInfoMapper dpBaseDealerInfoMapper;
//	@Resource
//	private DzGpsListMapper dzGpsListMapper;
//	@Resource
//	private RedisObjectClient redisObjectClient;
//	@Autowired
//	private IOtherHolidaysService otherHolidaysService;
//	@Autowired
//	private DhArchiveMapper dhArchiveMapper;
//	@Autowired
//	private YwBusinessDetailMapper ywBusinessDetailMapper;
	@Autowired
	private IFkAuditListService fkAuditListService;
	@Autowired
	private OtherAuditInfoMapper otherAuditInfoMapper;
	@Autowired
	private FkAuditListMapper fkAuditListMapper;
	@Autowired
	private YwBusinessLoanMapper ywBusinessLoanMapper;

	@Override
	public void saveUpdateAndDelete(YwBusinessCar ywBusinessCar, OperateType operType) {
		switch (operType) {
		case add:
			ywBusinessCar.initInsertData(ywBusinessCar.getModifier(), ywBusinessCar.getModifierName());
			ywBusinessCarMapper.insert(ywBusinessCar);
			break;
		case edit:
			ywBusinessCar.initUpdateData(ywBusinessCar.getModifier(), ywBusinessCar.getModifierName());
			// 影响的记录数
			int updateCount = ywBusinessCarMapper.updateByPrimaryKeySelective(ywBusinessCar);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywBusinessCar.initDeleteData(ywBusinessCar.getModifier(), ywBusinessCar.getModifierName());
			// 影响的记录数
			int deleteCount = ywBusinessCarMapper.updateByPrimaryKeySelective(ywBusinessCar);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwBusinessCar> selectByExt(YwBusinessCarExt ywBusinessCarExt) {
		return ywBusinessCarMapper.selectByExt(ywBusinessCarExt);
	}
	
	@Override
	public int countByExt(YwBusinessCarExt ywBusinessCarExt){
		return ywBusinessCarMapper.countByExt(ywBusinessCarExt);
	}
	
	@Override
	public YwBusinessCar selectById(String id) {
		return ywBusinessCarMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywBusinessCarMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwBusinessCar ywBusinessCar) {
		return ywBusinessCarMapper.countRefined(ywBusinessCar);
	}

	@Override
	public YwBusinessCar selectRefined(YwBusinessCar ywBusinessCar) {
		return ywBusinessCarMapper.selectRefined(ywBusinessCar);
	}
	
	@Override
	public int deletedRefined(YwBusinessCar ywBusinessCar) {
		return ywBusinessCarMapper.deletedRefined(ywBusinessCar);
	}

	@Override
	public boolean repeat(YwBusinessCar ywBusinessCar) {
		int count = countRefined(ywBusinessCar);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywBusinessCar.getId())) {
			return true;
		}
		YwBusinessCar dbItem = selectRefined(ywBusinessCar);
		if(null == dbItem) {
			return false;
		}
		return !ywBusinessCar.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywBusinessCarMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwBusinessCar> ywBusinessCars) {
		return ywBusinessCarMapper.insertBatch(ywBusinessCars);
	}
}
